<template>
  <h2>部门管理</h2>
  <el-button type="primary" 
    style="margin: 10px 0 10px 0;"
    @click="addDept"
  > + 添加部门</el-button>
  <!-- 表格 -->
  <el-table :data="deptTableData" border>
    <el-table-column type="index" width="80" align="center" label="序号" />
    <el-table-column prop="name" align="center" label="部门名称" />
    <el-table-column prop="updateTime" align="center" label="最后操作时间" />
    <el-table-column align="center" label="操作">
      <template #default="scope">
        <el-button size="small" type="primary" @click="handleEdit(scope.row.id)">编辑</el-button>
        <el-button size="small" type="danger" @click="handleDel(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 弹窗：新增/修改部门 -->
  <el-dialog 
    v-model="dialogFormVisible" 
    :title="formTilte" 
    width="400px"
  >
    <!-- 表单 -->
    <el-form :model="dept" :rules="rules" ref="ruleFormRef">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="dept.name"></el-input>
      </el-form-item>
    </el-form>
    <!-- 按钮 -->
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
  
</template>

<script setup>
import { ref, onMounted, reactive } from 'vue'
import { queryAllapi, addApi, queryByIdApi, updateApi, deleteApi } from '@/api/dept'
import { ElMessage, ElMessageBox } from 'element-plus'
const deptTableData = ref([])

onMounted(()=>{
  search()
})

// 查询所有数据
const search = async() => {
  const res = await queryAllapi()
  if (res.code) {
    deptTableData.value = res.data
  }
}

// 弹窗
const dialogFormVisible = ref(false) 
const formTilte = ref('')
const dept = ref({name: ''})
// 表单校验规则
const rules = reactive({
  name: [
    { required: true, message: '部门名称为必填项', trigger: 'blur' },
    { min: 2, max: 12, message: '字段长度为2~12', trigger: 'blur'}
  ]
})
const ruleFormRef = ref();

// 弹窗--新增数据
const addDept = () => {
  formTilte.value = '新增部门'
  dept.value = { name: '' } // 清空表单数据
  dialogFormVisible.value = true
  ruleFormRef.value?.resetFields() // 重置表单校验状态
}
const save = async() => {
  if (!ruleFormRef.value) return;
  await ruleFormRef.value.validate(async(valid, fields) => {
    if (valid) {
      let res = null;
      // 判断是新增还是修改
      if (dept.value.id) {
        res = await updateApi(dept.value)
      } else {
        res = await addApi(dept.value) 
      }
      if (res.code) {
        ElMessage.success("数据提交成功")
        search()
      } else {
        ElMessage.error(res.msg)
      }
      dialogFormVisible.value = false
    } else {
      ElMessage.error("表单不符合校验规则")
    }
  })
}

// 编辑数据
const handleEdit = async (id) => {
  formTilte.value = '编辑部门'
  dialogFormVisible.value = true
  dept.value = { name: '' } // 清空表单数据
  ruleFormRef.value?.resetFields()

  const res = await queryByIdApi(id)
  if (res.code) {
    dept.value = res.data
  }
}

// 删除数据
const handleDel = async (id) => {
  ElMessageBox.confirm('此操作将永久删除该部门，是否继续', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async() => {
    const res = await deleteApi(id)
    if (res.code) {
      ElMessage.success("数据删除成功")
      search()
    }
  })
}
</script>

<style scope>

</style>